﻿package com.tudou.ad.view.base
{
    import com.tudou.ad.*;
    import com.tudou.ad.components.*;
    import com.tudou.ad.components.events.*;
    import com.tudou.ad.consts.*;
    import com.tudou.ad.events.*;
    import com.tudou.track.*;
    import flash.events.*;
    import flash.utils.*;

    public class AdVideoViewBase extends AdFrontsViewBase
    {
        protected var videoHeight:Number;
        protected var videoWidth:Number;
        protected var floatBox:AdVideoFloatBox;
        protected var showAdAtStart:Boolean = false;
        protected var adVideo:TdAdVideo;
        protected var saveVolume:Boolean = false;

        public function AdVideoViewBase()
        {
            return;
        }// end function

        protected function floatBoxHlr(event:Event) : void
        {
            if (event.type == AdVideoFloatBox.CLOSE)
            {
                floatBox.visible = false;
            }
            else
            {
                adFloatClick();
            }
            return;
        }// end function

        protected function adBuffer() : void
        {
            if (controlSprite)
            {
                controlSprite.showSkip = true;
            }
            return;
        }// end function

        protected function initFloatBox() : void
        {
            floatBox = new AdVideoFloatBox();
            viewBox.addChild(floatBox);
            floatBox.addEventListener(AdVideoFloatBox.CLICK, floatBoxHlr);
            floatBox.addEventListener(AdVideoFloatBox.CLOSE, floatBoxHlr);
            return;
        }// end function

        protected function skipHlr(event:Event) : void
        {
            _adShowUserSkip();
            clearAd();
            return;
        }// end function

        protected function adClickHlr(event:Event) : void
        {
            trace("adClick");
            adClick();
            return;
        }// end function

        protected function startAdMian() : void
        {
            if (showAdAtStart)
            {
                _showAd();
            }
            if (adVideo)
            {
                if (saveVolume && !AdConsts.VIDEO_VOLUME_ON)
                {
                    adVideo.volume = 0;
                }
                adVideo.startAd();
            }
            else
            {
                _showAd();
                adShowStart();
                adShowNearEnd();
            }
            return;
        }// end function

        protected function _clearAd(param1:Boolean) : void
        {
            if (!clearAdCommon())
            {
                return;
            }
            isSkip = param1;
            if (isSkip)
            {
                _adShowSkip();
            }
            clearAdMian();
            return;
        }// end function

        protected function clearControlPanel() : void
        {
            if (timer)
            {
                timer.removeEventListener(TimerEvent.TIMER, timerEventHlr);
            }
            timer = null;
            if (controlSprite)
            {
                controlSprite.removeEventListener(AdVideoControlPanelEvent.TURN_ON, volumeHlr);
                controlSprite.removeEventListener(AdVideoControlPanelEvent.TURN_OFF, volumeHlr);
                controlSprite.removeEventListener(AdVideoControlPanelEvent.SKIP, skipHlr);
                controlSprite.clear();
                viewBox.removeChild(controlSprite);
            }
            controlSprite = null;
            return;
        }// end function

        override public function initToShow() : void
        {
            if (!initToShowCommon())
            {
                return;
            }
            isVideo = adItemInfo.isVideo;
            hasBg = adItemInfo.backUrl ? (true) : (false);
            hasFront = adItemInfo.url ? (true) : (false);
            videoWidth = mainWidth;
            videoHeight = mainHeight;
            if (hasBg)
            {
                initBgStuff();
            }
            if (isVideo)
            {
                initVideoStuff();
            }
            if (hasFront)
            {
                initFrontStuff();
            }
            if (adItemInfo.fLink)
            {
                initFloatBox();
            }
            if (adItemInfo.durationType > 0)
            {
                initControlPanel();
            }
            addToShowStuff();
            return;
        }// end function

        override protected function adShowComplete() : void
        {
            if (!isSkip)
            {
                _adShowComplete();
            }
            _clearAd(isSkip);
            return;
        }// end function

        protected function _showAd() : void
        {
            showAd();
            if (timer && !timer.running)
            {
                timer.reset();
                timer.start();
            }
            return;
        }// end function

        protected function timerEventHlr(event:TimerEvent) : void
        {
            var _loc_2:int = 0;
            if (!isVideo)
            {
                _loc_2 = 0;
                _loc_2 = Math.max(0, int(_countDownTime) - timer.currentCount);
                controlSprite.titleTime = _loc_2;
                var _loc_4:* = etime + 1;
                etime = _loc_4;
                if (timer.currentCount >= adItemInfo.durationType)
                {
                    adShowComplete();
                }
            }
            return;
        }// end function

        protected function clearBgStuff() : void
        {
            if (bgAdSprite)
            {
                bgAdSprite.removeEventListener(TudouAdSpriteEvent.CLICK, adClickHlr);
                bgAdSprite.removeAdDisplay();
                viewBox.removeChild(bgAdSprite);
            }
            bgStuff = null;
            bgAdSprite = null;
            return;
        }// end function

        override public function startAd() : void
        {
            if (!startAdCommon())
            {
                return;
            }
            startAdMian();
            return;
        }// end function

        protected function clearVideoStuff() : void
        {
            if (adVideo)
            {
                adVideo.removeEventListener(AdVideoEvent.CLICK, adClickHlr);
                adVideo.removeEventListener(AdVideoEvent.SHOW_START, adVideoHlr);
                adVideo.removeEventListener(AdVideoEvent.SHOW_END, adVideoHlr);
                adVideo.removeEventListener(AdVideoEvent.SHOW_NEAR_END, adVideoHlr);
                adVideo.removeEventListener(AdVideoEvent.BUFFER, adVideoHlr);
                adVideo.removeEventListener(AdVideoEvent.TIME, adVideoHlr);
                adVideo.clearAd();
                viewBox.removeChild(adVideo);
            }
            adVideo = null;
            return;
        }// end function

        override public function clearAd() : void
        {
            _clearAd(true);
            return;
        }// end function

        protected function initVideoStuff() : void
        {
            adVideo = new TdAdVideo(videoWidth, videoHeight);
            adVideo.adItemInfo = adItemInfo;
            adVideo.adThrowInfo = adThrowInfo;
            adVideo.tracker = _tracker;
            adVideo.initToShow();
            viewBox.addChild(adVideo);
            adVideo.addEventListener(AdVideoEvent.CLICK, adClickHlr);
            adVideo.addEventListener(AdVideoEvent.SHOW_START, adVideoHlr);
            adVideo.addEventListener(AdVideoEvent.SHOW_END, adVideoHlr);
            adVideo.addEventListener(AdVideoEvent.SHOW_NEAR_END, adVideoHlr);
            adVideo.addEventListener(AdVideoEvent.BUFFER, adVideoHlr);
            adVideo.addEventListener(AdVideoEvent.TIME, adVideoHlr);
            return;
        }// end function

        override public function goTransform(param1:uint, param2:uint, param3:Boolean) : void
        {
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            _isFullscreen = param3;
            if (param3)
            {
                mainWidth = bgWidth;
                mainHeight = bgHeight;
                _loc_4 = 0;
                _loc_5 = 0;
            }
            else
            {
                mainWidth = param1;
                mainHeight = param2;
                _loc_4 = (bgWidth - mainWidth) / 2;
                _loc_5 = mainTop;
            }
            if (bgAdSprite)
            {
                bgAdSprite.visible = isShowBg;
                bgAdSprite.setWidthHeight(bgWidth, bgHeight);
            }
            if (frontAdSprite)
            {
                frontAdSprite.x = _loc_4;
                frontAdSprite.y = _loc_5;
                frontAdSprite.setWidthHeight(mainWidth, mainHeight);
            }
            if (adVideo)
            {
                adVideo.x = _loc_4;
                adVideo.y = _loc_5;
                adVideo.goTransform(mainWidth, mainHeight, param3);
            }
            if (controlSprite)
            {
                controlSprite.x = _loc_4;
                controlSprite.y = 0;
                controlSprite.width = mainWidth;
            }
            if (floatBox)
            {
                floatBox.x = int(_loc_4 + mainWidth - floatBox.width);
                floatBox.y = int(_loc_5 + mainHeight - floatBox.height);
                floatBox.visible = !isShowBg;
            }
            return;
        }// end function

        protected function initControlPanel() : void
        {
            controlSprite = new AdVideoControlPanel();
            viewBox.addChild(controlSprite);
            controlSprite.showVolume = isVideo;
            if (saveVolume)
            {
                controlSprite.volumeOn = AdConsts.VIDEO_VOLUME_ON;
            }
            controlSprite.width = mainWidth;
            timer = new Timer(1000);
            timer.addEventListener(TimerEvent.TIMER, timerEventHlr);
            controlSprite.titleTime = _countDownTime;
            controlSprite.addEventListener(AdVideoControlPanelEvent.TURN_ON, volumeHlr);
            controlSprite.addEventListener(AdVideoControlPanelEvent.TURN_OFF, volumeHlr);
            controlSprite.addEventListener(AdVideoControlPanelEvent.SKIP, skipHlr);
            return;
        }// end function

        protected function adTime(param1:Number) : void
        {
            if (adItemInfo.durationType <= 0)
            {
                return;
            }
            etime = int(param1);
            var _loc_2:int = 0;
            _loc_2 = Math.max(0, int(_countDownTime) - etime);
            if (controlSprite)
            {
                controlSprite.titleTime = _loc_2;
            }
            return;
        }// end function

        protected function volumeHlr(event:Event) : void
        {
            if (event.type == AdVideoControlPanelEvent.TURN_OFF)
            {
                adVideo.volume = 0;
                controlSprite.volumeOn = false;
                if (saveVolume)
                {
                    AdConsts.VIDEO_VOLUME_ON = false;
                }
                adVolumeClick(false);
            }
            else if (event.type == AdVideoControlPanelEvent.TURN_ON)
            {
                adVideo.volume = 0.5;
                controlSprite.volumeOn = true;
                if (saveVolume)
                {
                    AdConsts.VIDEO_VOLUME_ON = true;
                }
                adVolumeClick(true);
            }
            return;
        }// end function

        protected function adVideoHlr(event:AdVideoEvent) : void
        {
            switch(event.type)
            {
                case AdVideoEvent.SHOW_START:
                {
                    _showAd();
                    if (frontAdSprite)
                    {
                        frontAdSprite.visible = false;
                    }
                    adShowStart();
                    break;
                }
                case AdVideoEvent.SHOW_NEAR_END:
                {
                    adShowNearEnd();
                    break;
                }
                case AdVideoEvent.SHOW_END:
                {
                    adTime(Math.round(adVideo.playheadTime));
                    isSkip = adVideo.isSkip;
                    adShowComplete();
                    break;
                }
                case AdVideoEvent.BUFFER:
                {
                    adBuffer();
                    break;
                }
                case AdVideoEvent.TIME:
                {
                    adTime(adVideo.playheadTime);
                    break;
                }
                default:
                {
                    break;
                    break;
                }
            }
            return;
        }// end function

        protected function clearAdMian() : void
        {
            hideAd();
            clearComps();
            countDownTime = 0;
            adItemInfo = null;
            adShowEnd();
            return;
        }// end function

        protected function adVolumeClick(param1:Boolean) : void
        {
            sendTrack(NodeLog.AD_VIDEO_VOLUME_CLICK, [param1 ? (1) : (0)]);
            return;
        }// end function

        protected function get isShowBg() : Boolean
        {
            return !_isFullscreen && !_isExpand;
        }// end function

        protected function clearFloatBox() : void
        {
            if (floatBox)
            {
                floatBox.removeEventListener(AdVideoFloatBox.CLICK, floatBoxHlr);
                floatBox.removeEventListener(AdVideoFloatBox.CLOSE, floatBoxHlr);
                floatBox.clear();
                viewBox.removeChild(floatBox);
            }
            floatBox = null;
            return;
        }// end function

        override protected function clearComps() : void
        {
            clearControlPanel();
            clearVideoStuff();
            clearFrontStuff();
            clearBgStuff();
            clearFloatBox();
            clearCompsCommon();
            return;
        }// end function

        protected function initBgStuff() : void
        {
            bgAdSprite = new TudouAdSprite(bgWidth, bgHeight, 0, 0);
            viewBox.addChild(bgAdSprite);
            bgAdSprite.addEventListener(TudouAdSpriteEvent.CLICK, adClickHlr);
            return;
        }// end function

    }
}
